package com.fengmi.utils;

import cn.hutool.core.io.resource.ResourceUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fengmi.utils.security.JwtUtils;
import com.fengmi.utils.security.RsaUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.ResourceUtils;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：陈学虎
 * 时间：2021/7/13 0013 下午 8:20
 */
public class jwtTest {
//    public static void main(String[] args) throws Exception{
//        Calendar c = Calendar.getInstance();
//        c.add(Calendar.SECOND,5);
//        //生产jwt令牌
//        String jwtToken = JWT.create()
////        builder.withHeader();//默认使用
//                .withClaim("id","1")
//                .withClaim("name","张三")
//                .withClaim("age",18)
//                .withExpiresAt(c.getTime()) //指定令牌过期时间
//                .sign(Algorithm.HMAC256("cxh"));
//        System.out.println(jwtToken);
//
//        Thread.sleep(6000);
//        //解析令牌
//        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256("cxh")).build();
//        DecodedJWT verify = jwtVerifier.verify(jwtToken); //效验令牌
//        //解析令牌,获取用户信息
//        String id = verify.getClaim("id").asString();
//        System.out.println("id"+"------"+id);
//
//        String name = verify.getClaim("name").asString();
//        String age = verify.getClaim("age").asString();
//        System.out.println(name+"----"+age);
//    }

//    生成公钥和私钥
//    public static void main(String[] args) throws Exception{
//        String privateFilePath = "C:\\Users\\Administrator\\Desktop\\16.项目\\key\\rsa";
//        String publicFilePath = "C:\\Users\\Administrator\\Desktop\\16.项目\\key\\rsa.pub";
//
//        RsaUtils.generateKey(publicFilePath,privateFilePath,"cxh",2048);
//    }

    public static void main(String[] args) throws Exception{
//        //生产jwt令牌
//        Map userInfo = new HashMap(){{
//            put("name","jack");
//            put("age",18);
//        }};
//
//        //获取私钥
//        String path = ResourceUtils.getFile("classpath:rsa").getPath();
//        //构建私钥对象
//        PrivateKey privateKey = RsaUtils.getPrivateKey(path);
////        私钥加密token
//        String token = JwtUtils.generateTokenExpireInMinutes(userInfo, privateKey, 1);
//        System.out.println(token);
//
//        //解析token
////        获取公钥路径
//        String path1 = ResourceUtils.getFile("classpath:rsa.pub").getPath();
//        //构建公钥对象
//        PublicKey publicKey = RsaUtils.getPublicKey(path1);
//        Map infoFromToken = (Map) JwtUtils.getInfoFromToken(token, publicKey, Map.class);
//        System.out.println(infoFromToken.get("name")+"---->"+infoFromToken.get("age"));

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("123456");
        System.out.println(encode);
    }

}
